package com.stockz.downloaderx.callback;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import com.stockz.core.util.CoreConstant;
import com.stockz.core.util.Global;
import com.stockz.downloaderx.Download;
import com.stockz.downloaderx.Downloader.Callback;
import com.stockz.ui.swing.core.MainFrame;
import com.stockz.ui.swing.util.JLabelTextSetter;

@Component
public class UIUpdateCallback implements Callback {

	@Autowired
	private MessageSource messageSource;
	
	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public void execute(Download download) {
		if(download != null && download.getEvent() != null && download.getType() != null ){
			if(download.getEvent().equals(EVENT.ALL) && download.getType().equals(TYPE.START)){
				updateUI("downloader.all.start.message",download.getDownloadDate(), download.getExchange().getName());
			}else if(download.getEvent().equals(EVENT.LOOP) && download.getType().equals(TYPE.START)){
				updateUI("downloader.loop.start.message",download.getDownloadDate(), download.getExchange().getName());
			}else if(download.getEvent().equals(EVENT.LOOP) && download.getType().equals(TYPE.SUCCESS)){
				updateUI("downloader.loop.success.message",download.getDownloadDate(), download.getExchange().getName());
			}else if(download.getEvent().equals(EVENT.LOOP) && download.getType().equals(TYPE.FAIL)){
				updateUI("downloader.loop.fail.message",download.getDownloadDate(), download.getExchange().getName());
			}else if(download.getEvent().equals(EVENT.ALL) && download.getType().equals(TYPE.SUCCESS)){
				updateUI("downloader.all.success.message",download.getStartDate(), download.getEndDate());
			}else if(download.getEvent().equals(EVENT.ALL) && download.getType().equals(TYPE.FAIL)){
				updateUI("downloader.all.fail.message",download.getDownloadDate(), download.getExchange().getName());
			}
		}
	}

	private void updateUI(String key, Object... args){
		JLabelTextSetter.setText(MainFrame.getInstance().getMessageLabel(), 
				messageSource.getMessage(key, args,(Locale)Global.get(CoreConstant.keyCurrentLocale)));
	}
	
}
